package com.handinfo.android.core.object;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Enumeration;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.resource.DWSerializable;
import com.handinfo.android.core.resource.DWSerializableFactory;
import com.handinfo.android.core.resource.ResHead;
import com.handinfo.android.core.resource.Resource;
import com.handinfo.android.core.resource.ResourceManager;
import com.handinfo.android.game.GameActor;
import com.handinfo.android.utils.Tools;

public class Actor extends DWSerializable
{
	public Scene m_scene;
	public int m_id;
	public Animation m_anim;
	public Animation[] m_anims;
	public int m_actionIDNow;
	public int m_actionIDBefore;
	public int m_actionIDNext;
	public int m_frameId;
	public int m_frameIndex;
	public int m_frameDuration;
	public boolean m_actionCycle = true;
	public boolean m_actionOver;
	public int m_layer;
	public int m_phyLayer, m_prePhyLayer;
	public int m_flag;
	public boolean m_flipX, m_flipY;
	public boolean m_visible;
	public boolean m_enabled;
	public boolean m_needDraw;
	public boolean m_isOnce;
	public short[] m_collideBox = new short[4];
	public short[] m_activeBox = new short[4];
	public short[] m_parameters = new short[0];

	public long m_gameId = 0;

	public int m_posX; // X坐标
	public int m_posY; // Y坐标
	public int m_prevPosX; // 上一帧X坐标
	public int m_prevPosY; // 上一帧Y坐标
	public int m_destX; // 目的地X坐标
	public int m_destY; // 目的地Y坐标
	public float m_velocity = 10; // 速度
	public float m_vX; // X方向每帧位移
	public float m_vY; // Y方向每帧位移
	public float m_preVX; // 上次设置的速度X
	public float m_preVY; // 上次设置的速度Y
	public int m_phylayer; // 元件所在物理层索引 (-1：随意行走 0:无法通过 1-4：互斥，只能通往-1，9，10，11
	// 8：最高层，无视任何物理层碰撞 9-11：衔接互斥的两层)
//	public static final int ACTOR_FRACTION_BIT = 10;

	public static final int ACTOR_FLAG_CG = 1 << 4; // 是否过场动画元件
	public static final int ACTOR_FLAG_EVENT = 1 << 5; // 是否触发事件
	public static final int ACTOR_FLAG_SELF = 1 << 6; // 是否玩家控制角色

	protected final static int COL_TOP = 1;
	protected final static int COL_BOTTOM = 2;
	protected final static int COL_LEFT = 4;
	protected final static int COL_RIGHT = 8;

	public Actor()
	{

	}

	public Actor(Resource resource)
	{
		m_resource = resource;
	}

	public Actor(Animation anim)
	{
		m_anims = new Animation[1];
		m_anims[0] = anim;
		m_anim = m_anims[0];
		m_enabled = false;
		m_visible = false;
	}

	public Actor(Animation[] anims)
	{
		m_anims = anims;
		m_anim = anims[0];
		m_enabled = false;
		m_visible = false;
	}

	public Actor(Scene scene)
	{
		this.m_scene = scene;
	}

	/**
	 * 克隆元件对象，克隆对象为自己
	 * 
	 * @return
	 */
	public Actor clone()
	{
		return clone(this);
	}

	/**
	 * 克隆给定的元件对象
	 * 
	 * @param actor
	 * @return
	 */
	public Actor clone(Actor actor)
	{
		Actor ca = new Actor();
		ca.m_scene = actor.m_scene;
		ca.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, ca.m_anims, 0, actor.m_anims.length);
		ca.m_anim = actor.m_anim;
		ca.m_actionIDNow = actor.m_actionIDNow;
		ca.m_actionIDBefore = actor.m_actionIDBefore;
		ca.m_frameId = actor.m_frameId;
		ca.m_frameIndex = actor.m_frameIndex;
		ca.m_frameDuration = actor.m_frameDuration;
		ca.m_actionCycle = actor.m_actionCycle;
		ca.m_actionOver = actor.m_actionOver;
		ca.m_layer = actor.m_layer;
		ca.m_phyLayer = actor.m_phyLayer;
		ca.m_flag = actor.m_flag;
		ca.m_flipX = actor.m_flipX;
		ca.m_flipY = actor.m_flipY;
		ca.m_visible = actor.m_visible;
		ca.m_enabled = actor.m_enabled;
		// ca.m_movable = actor.m_movable;
		ca.m_needDraw = actor.m_needDraw;
		ca.m_posX = actor.m_posX;
		ca.m_posY = actor.m_posY;
		ca.m_vX = actor.m_vX;
		ca.m_vY = actor.m_vY;
		ca.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, ca.m_collideBox, 0,
				actor.m_collideBox.length);
		ca.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, ca.m_activeBox, 0,
				actor.m_activeBox.length);
		ca.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, ca.m_parameters, 0,
				actor.m_parameters.length);
		ca.m_resHead = actor.m_resHead;
		ca.m_resource = actor.m_resource;
		ca.m_refsResHead = new ResHead[actor.m_refsResHead.length];
		System.arraycopy(actor.m_refsResHead, 0, ca.m_refsResHead, 0,
				actor.m_refsResHead.length);
		return ca;
	}

	@Override
	public void read(Resource res, DataInputStream dis) throws IOException
	{
		readHead(res, dis);
		loadAnimations(res);
		m_flag = dis.readInt();
		m_flipX = (m_flag & 0x01) != 0;
		m_flipY = (m_flag & 0x02) != 0;
		m_enabled = (m_flag & 0x04) != 0;
		m_visible = (m_flag & 0x08) != 0;
		int action = dis.readByte();
		if (action < 0) {
			action += 256;
		}
		setAction(action, true);
		m_collideBox[0] = dis.readShort();
		m_collideBox[1] = dis.readShort();
		m_collideBox[2] = (short) (dis.readShort() + m_collideBox[0]);
		m_collideBox[3] = (short) (dis.readShort() + m_collideBox[1]);
		m_activeBox[0] = dis.readShort();
		m_activeBox[1] = dis.readShort();
		m_activeBox[2] = (short) (dis.readShort() + m_activeBox[0]);
		m_activeBox[3] = (short) (dis.readShort() + m_activeBox[1]);
		int temp_parameterCount = dis.readByte();
		if (temp_parameterCount != 0) {
			m_parameters = new short[temp_parameterCount];
			for (int j = 0; j < temp_parameterCount; j++) {
				m_parameters[j] = dis.readShort();
			}
		}
		checkLoadFinished();
	}

	/**
	 * 根据指定Actor初始化自身部分属性
	 * 
	 * @param actor
	 */
	public void copy(Actor actor)
	{
		this.m_scene = actor.m_scene;
		this.m_anims = new Animation[actor.m_anims.length];
		System.arraycopy(actor.m_anims, 0, this.m_anims, 0, actor.m_anims.length);
		this.m_anim = actor.m_anim;
		this.m_actionIDNow = actor.m_actionIDNow;
		this.m_actionIDBefore = actor.m_actionIDBefore;
		this.m_frameId = actor.m_frameId;
		this.m_frameIndex = actor.m_frameIndex;
		this.m_frameDuration = actor.m_frameDuration;
		this.m_actionCycle = actor.m_actionCycle;
		this.m_actionOver = actor.m_actionOver;
		this.m_layer = actor.m_layer;
		this.m_phyLayer = actor.m_phyLayer;
		this.m_flag = actor.m_flag;
		this.m_flipX = actor.m_flipX;
		this.m_flipY = actor.m_flipY;
		this.m_visible = actor.m_visible;
		this.m_enabled = actor.m_enabled;
		// ca.m_movable = actor.m_movable;
		this.m_needDraw = actor.m_needDraw;
		this.m_posX = actor.m_posX;
		this.m_posY = actor.m_posY;
		this.m_vX = actor.m_vX;
		this.m_vY = actor.m_vY;
		this.m_collideBox = new short[actor.m_collideBox.length];
		System.arraycopy(actor.m_collideBox, 0, this.m_collideBox, 0,
				actor.m_collideBox.length);
		this.m_activeBox = new short[actor.m_activeBox.length];
		System.arraycopy(actor.m_activeBox, 0, this.m_activeBox, 0,
				actor.m_activeBox.length);
		this.m_parameters = new short[actor.m_parameters.length];
		System.arraycopy(actor.m_parameters, 0, this.m_parameters, 0,
				actor.m_parameters.length);
		this.m_resHead = actor.m_resHead;
		this.m_resource = actor.m_resource;
		this.m_refsResHead = new ResHead[actor.m_refsResHead.length];
		System.arraycopy(actor.m_refsResHead, 0, this.m_refsResHead, 0,
				actor.m_refsResHead.length);
	}

	/**
	 * 设置隐藏/显示标记
	 * 
	 * @param visible
	 * boolean
	 */
	public void setVisible(boolean visible)
	{
		this.m_visible = visible;
	}

	/**
	 * 设置有效标记
	 * 
	 * @param enabled
	 * boolean
	 */
	public void setEnabled(boolean enabled)
	{
		this.m_enabled = enabled;
	}

	/**
	 * 设置元件参数值
	 * 
	 * @param index
	 * int
	 * @param value
	 * int
	 */
	public void setParameter(int index, int value)
	{
		if (index >= m_parameters.length) {
			return;
		}
		m_parameters[index] = (short) value;
	}

	/**
	 * 获得元件参数值
	 * 
	 * @param index
	 * int
	 * @return short
	 */
	public short getParameter(int index)
	{
		if (index >= m_parameters.length) {
			return -1;
		}
		return m_parameters[index];
	}

	public void activate()
	{
		m_enabled = true;
		m_visible = true;
	}

	public void deactivate()
	{
		m_enabled = false;
		m_visible = false;
	}

	/**
	 * 设定元件动作
	 * 
	 * @param actionIDNext
	 * @param mustChange
	 */
	public void setAction(int actionIDNext, boolean mustChange)
	{
		try {
			if (m_anim == null) {
//				Tools.debugPrintln("Actor setAction(): anim == null!");
				return;
			}
			if (m_anim.m_actions == null) {
				Tools.debugPrintln("Actor setAction(): actions == null!");
				return;
			}
			if (!mustChange) {
				if (m_actionIDNow == actionIDNext) {
					return;
				}
			}
			if (m_actionIDNow != actionIDNext) {
				m_actionIDBefore = m_actionIDNow;
			}
			m_actionIDNow = actionIDNext;
			m_frameId = 0;
			if (m_actionIDNow < 0) {
				Tools.debugPrintln("Actor setAction(): actionIDNow < 0!" + "-- actionId: "
						+ m_actionIDNow);
			}
			else if (m_actionIDNow < m_anim.m_actions.length) {
				if (m_anim.m_actions[m_actionIDNow].length != 0) {
					m_frameIndex = m_anim.m_actions[m_actionIDNow][0] & 0x03FF;
				}
				else {
					Tools.debugPrintln("Actor setAction(): action has no frame! " + "-- actionId: "
							+ m_actionIDNow);
				}
			}
			else {
				m_frameIndex = m_anim.m_actions[m_actionIDNow
						% m_anim.m_actions.length][0] & 0x03FF;
				Tools.debugPrintln("Actor setAction(): actionIDNow >= m_anim.m_actions.length "
						+ m_actionIDNow + " : " + (m_resHead == null ? "null" : m_resHead.m_url));
			}

			m_frameDuration = 0;
			m_actionOver = false;
		}
		catch (Exception ex) {
			Tools.debug(ex);
		}
	}

	public void setAction(int actionIDNext, boolean actionCycle,
			boolean mustChange)
	{
		setAction(actionIDNext, mustChange);
		m_actionCycle = actionCycle;
	}

	public void setAction(int actionIDNext, float vx, float vy, boolean actionCycle, boolean mustChange)
	{
		setAction(actionIDNext, mustChange);
		if (m_vX != vx || m_vY != vy) {
			m_preVX = m_vX;
			m_preVY = m_vY;
		}
		m_vX = vx;
		m_vY = vy;
		m_actionCycle = actionCycle;
	}

	public void setAction(int actionIDNext, int vx, int vy, boolean actionCycle, boolean mustChange)
	{
		setAction(actionIDNext, mustChange);
		if (m_vX != vx || m_vY != vy) {
			m_preVX = m_vX;
			m_preVY = m_vY;
		}
		m_vX = vx;
		m_vY = vy;
		m_actionCycle = actionCycle;
	}

	/**
	 * 将元件动作设置到指定的帧
	 * 
	 * @param frameID
	 * int
	 */
	public void setFrame(int frameID)
	{
		// 取余数从而达到循环执行动作帧的效果
		frameID %= m_anim.m_actions[m_actionIDNow].length;
		m_frameId = frameID;
		// 设置帧索引和帧持续帧数
		m_frameIndex = (m_anim.m_actions[m_actionIDNow][frameID] & 0x03FF);
		m_frameDuration = 0;
	}

	/**
	 * 设定元件动作，同时设置到特定帧
	 * 
	 * @param actionIDNext
	 * 动作ID
	 * @param frameID
	 * 帧
	 */
	public void setActionFrame(int actionIDNext, int frameID)
	{
		setAction(actionIDNext, true);
		setFrame(frameID);
	}

	/**
	 * 指向动作上一帧
	 */
	public void preFrame()
	{
		m_frameDuration--;
		if (m_anim == null
				|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
			return;
		}

		m_frameId--;
		if (m_frameId < 0) {
			m_actionOver = true;
			if (m_actionCycle) {
				m_frameId = m_anim.m_actions[m_actionIDNow].length - 1;
			}
			else {
				m_frameId++;
			}
		}

		m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
		m_frameDuration = 0;
	}

	/**
	 * 指向动作下一帧
	 */
	public void nextFrame()
	{
		m_frameDuration++;

		try {

			if (m_anim == null
					|| m_anim.m_actions == null
					|| m_actionIDNow >= m_anim.m_actions.length
					|| m_frameId >= m_anim.m_actions[m_actionIDNow].length
					|| m_frameDuration < ((m_anim.m_actions[m_actionIDNow][m_frameId] & 0xFFFF) >> 10)) {
				return;
			}
			m_frameId++;
			if (m_anim.m_actions.length > m_actionIDNow) {
				if (m_frameId >= m_anim.m_actions[m_actionIDNow].length) {
					m_actionOver = true;
					if (m_actionCycle) {
						m_frameId = 0;
					}
					else {
						m_frameId--;
					}
					// 一次性元件
					if (m_isOnce) {
						m_enabled = false;
					}
				}
				m_frameIndex = (m_anim.m_actions[m_actionIDNow][m_frameId] & 0x03FF);
				m_frameDuration = 0;
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	/**
	 * 获取当前Action的总持续时间
	 * 
	 * @return int
	 */
	public int getCurActionTime()
	{
		int time = 0;
		try {
			for (int i = 0; i < m_anim.m_actions[m_actionIDNow].length; i++) {
				time += ((m_anim.m_actions[m_actionIDNow][i] & 0xFFFF) >> 10);
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
		return time;
	}

	public void logic()
	{

	}

	/**
	 * 元件绘制
	 * 
	 * @param g
	 * @param camX
	 * @param camY
	 */
	public void draw(DWGraphics g, int camX, int camY)
	{
		drawAnimations(g, camX, camY);
	}

	/**
	 * 元件绘制
	 * @param g
	 * @param camX
	 * @param camY
	 * @param scaleX 放缩比例
	 * @param scaleY
	 */
	public void draw(DWGraphics g, int camX, int camY, float scaleX, float scaleY)
	{
		drawAnimations(g, camX, camY, scaleX, scaleY);
	}

	/**
	 * 绘制动画列表
	 * @param g
	 * @param camX
	 * @param camY
	 */
	public void drawAnimations(DWGraphics g, int camX, int camY)
	{
		if (m_anims == null || m_anims.length == 0)
			return;
		int x = m_posX - camX;
		int y = m_posY - camY;
		for (int i = 0; i < m_anims.length; i++) {
			if (m_anims[i] != null) {
				m_anims[i].draw(g, x, y, m_flipX, m_flipY, m_frameIndex);
			}
		}
	}

	/**
	 * 绘制动画列表
	 * @param g
	 * @param camX
	 * @param camY
	 */
	public void drawAnimations(DWGraphics g, int camX, int camY, float scaleX, float scaleY)
	{
		if (m_anims == null || m_anims.length == 0)
			return;
		int x = m_posX - camX;
		int y = m_posY - camY;
		for (int i = 0; i < m_anims.length; i++) {
			if (m_anims[i] != null) {
				m_anims[i].draw(g, x, y, m_flipX, m_flipY, m_frameIndex, scaleX, scaleY);
			}
		}
	}

	/**
	 * 元件位置移动
	 */
	public void updatePos()
	{
		m_prevPosX = m_posX;
		m_prevPosY = m_posY;
		m_posX += m_vX;
		m_posY += m_vY;
		if (m_scene == null || m_activeBox == null) {
			return;
		}
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
						+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
						+ m_scene.m_cameraH);

	}

	/**
	 * 设置元件位置
	 * 
	 * @param x
	 * @param y
	 */
	public void setPosition(int x, int y)
	{
		m_prevPosX = m_posX;
		m_prevPosY = m_posY;
		m_posX = x;
		m_posY = y;
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
						+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
						+ m_scene.m_cameraH);
	}

	/**
	 * 恢复元件位置
	 */
	public void restorePos()
	{
		m_posX = m_prevPosX;
		m_posY = m_prevPosY;
		m_needDraw = (m_posX + m_activeBox[2] >= m_scene.m_cameraX)
				&& (m_posX + m_activeBox[0] <= m_scene.m_cameraX
						+ m_scene.m_cameraW)
				&& (m_posY + m_activeBox[3] >= m_scene.m_cameraY)
				&& (m_posY + m_activeBox[1] <= m_scene.m_cameraY
						+ m_scene.m_cameraH);
	}

	/***
	 * 判断是否与指定的元件碰撞
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean collide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_posX + m_collideBox[0];
		int y1 = m_posY + m_collideBox[1];
		int x2 = m_posX + m_collideBox[2];
		int y2 = m_posY + m_collideBox[3];
		int ox1 = actor.m_posX + actor.m_collideBox[0];
		int oy1 = actor.m_posY + actor.m_collideBox[1];
		int ox2 = actor.m_posX + actor.m_collideBox[2];
		int oy2 = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 与指定Actor进行帧VS帧的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean frameCollideFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null || actor.m_anim == null
				|| m_anim.m_framesActiveBox.length == 0)
			return false;
		if (m_anim == null || m_anim.m_framesCollideBox.length == 0) {
			return false;
		}
		int x1 = m_posX + m_anim.m_framesCollideBox[m_frameIndex][0];
		int y1 = m_posY + m_anim.m_framesCollideBox[m_frameIndex][1];
		int x2 = m_posX + m_anim.m_framesCollideBox[m_frameIndex][2];
		int y2 = m_posY + m_anim.m_framesCollideBox[m_frameIndex][3];
		int ox1 = actor.m_posX
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][0];
		int oy1 = actor.m_posY
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][1];
		int ox2 = actor.m_posX
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][2];
		int oy2 = actor.m_posY
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 与指定Actor进行帧VSActor的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean frameCollideActor(Actor actor, boolean ignoreLayer)
	{
		if (actor == null)
			return false;
		if (m_anim == null || m_anim.m_framesCollideBox.length == 0) {
			return false;
		}
		int x1 = m_posX + m_anim.m_framesCollideBox[m_frameIndex][0];
		int y1 = m_posY + m_anim.m_framesCollideBox[m_frameIndex][1];
		int x2 = m_posX + m_anim.m_framesCollideBox[m_frameIndex][2];
		int y2 = m_posY + m_anim.m_framesCollideBox[m_frameIndex][3];
		int ox1 = actor.m_posX + actor.m_collideBox[0];
		int oy1 = actor.m_posY + actor.m_collideBox[1];
		int ox2 = actor.m_posX + actor.m_collideBox[2];
		int oy2 = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 与指定Actor进行ActorVS帧的碰撞判断
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * @return
	 */
	public boolean actorCollideFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null || actor.m_anim == null
				|| actor.m_anim.m_framesCollideBox == null)
			return false;
		int x1 = m_posX + m_collideBox[0];
		int y1 = m_posY + m_collideBox[1];
		int x2 = m_posX + m_collideBox[2];
		int y2 = m_posY + m_collideBox[3];
		int ox1 = actor.m_posX
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][0];
		int oy1 = actor.m_posY
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][1];
		int ox2 = actor.m_posX
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][2];
		int oy2 = actor.m_posY
				+ actor.m_anim.m_framesCollideBox[actor.m_frameIndex][3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断与指定的actor下一帧是否碰撞
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean collideNextFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		float x1 = m_posX + m_collideBox[0] + m_vX;
		float y1 = m_posY + m_collideBox[1] + m_vY;
		float x2 = m_posX + m_collideBox[2] + m_vX;
		float y2 = m_posY + m_collideBox[3] + m_vY;
		float ox1 = actor.m_posX + actor.m_collideBox[0]
				+ actor.m_vX;
		float oy1 = actor.m_posY + actor.m_collideBox[1]
				+ actor.m_vY;
		float ox2 = actor.m_posX + actor.m_collideBox[2]
				+ actor.m_vX;
		float oy2 = actor.m_posY + actor.m_collideBox[3]
				+ actor.m_vY;
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断与指定的actor上一帧是否碰撞
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean collidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_prevPosX + m_collideBox[0];
		int y1 = m_prevPosY + m_collideBox[1];
		int x2 = m_prevPosX + m_collideBox[2];
		int y2 = m_prevPosY + m_collideBox[3];
		int ox1 = actor.m_prevPosX + actor.m_collideBox[0];
		int oy1 = actor.m_prevPosY + actor.m_collideBox[1];
		int ox2 = actor.m_prevPosX + actor.m_collideBox[2];
		int oy2 = actor.m_prevPosY + actor.m_collideBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断当前帧自身和指定Actor的ActiveBox是否相交
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean activeCollide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_posX + m_activeBox[0];
		int y1 = m_posY + m_activeBox[1];
		int x2 = m_posX + m_activeBox[2];
		int y2 = m_posY + m_activeBox[3];
		int ox1 = actor.m_posX + actor.m_activeBox[0];
		int oy1 = actor.m_posY + actor.m_activeBox[1];
		int ox2 = actor.m_posX + actor.m_activeBox[2];
		int oy2 = actor.m_posY + actor.m_activeBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断上一帧自身和指定Actor的ActiveBox是否相交
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean activeCollidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_prevPosX + m_activeBox[0];
		int y1 = m_prevPosY + m_activeBox[1];
		int x2 = m_prevPosX + m_activeBox[2];
		int y2 = m_prevPosY + m_activeBox[3];
		int ox1 = actor.m_prevPosX + actor.m_activeBox[0];
		int oy1 = actor.m_prevPosY + actor.m_activeBox[1];
		int ox2 = actor.m_prevPosX + actor.m_activeBox[2];
		int oy2 = actor.m_prevPosY + actor.m_activeBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断当前帧自身activeBox和指定Actor的collideBox是否相交
	 * 
	 * @param actor
	 * @param ignoreLayer
	 * 是否忽略元件层
	 * @return
	 */
	public boolean aToCCollide(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_posX + m_activeBox[0];
		int y1 = m_posY + m_activeBox[1];
		int x2 = m_posX + m_activeBox[2];
		int y2 = m_posY + m_activeBox[3];
		int ox1 = actor.m_posX + actor.m_collideBox[0];
		int oy1 = actor.m_posY + actor.m_collideBox[1];
		int ox2 = actor.m_posX + actor.m_collideBox[2];
		int oy2 = actor.m_posY + actor.m_collideBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 判断上一帧自身activeBox和指定Actor的collideBox是否相交
	 * 
	 * @param actor
	 * Actor
	 * @param ignoreLayer
	 * boolean
	 * @return boolean
	 */
	public boolean aToCCollidePreFrame(Actor actor, boolean ignoreLayer)
	{
		if (actor == null) {
			return false;
		}
		int x1 = m_prevPosX + m_activeBox[0];
		int y1 = m_prevPosY + m_activeBox[1];
		int x2 = m_prevPosX + m_activeBox[2];
		int y2 = m_prevPosY + m_activeBox[3];
		int ox1 = actor.m_prevPosX + actor.m_collideBox[0];
		int oy1 = actor.m_prevPosY + actor.m_collideBox[1];
		int ox2 = actor.m_prevPosX + actor.m_collideBox[2];
		int oy2 = actor.m_prevPosY + actor.m_collideBox[3];
		boolean result = Tools.collide(x1, y1, x2, y2, ox1, oy1, ox2, oy2);
		if (ignoreLayer) { // 忽略元件层
			return result;
		}
		else {
			return (m_layer == actor.m_layer && result);
		}
	}

	/**
	 * 元件与背景层物理层的碰撞逻辑处理
	 */
	public void bgCollide()
	{
		float collideX = m_posX + m_vX;
		float collideY = m_posY + m_vY;

		int tileX = (int) collideX / m_scene.m_bg.m_tileW;
		int tileY = (int) collideY / m_scene.m_bg.m_tileH;
		tileX = Tools.limit(tileX, 0, m_scene.m_bg.m_columns - 1);
		tileY = Tools.limit(tileY, 0, m_scene.m_bg.m_rows - 1);

		int collideNum = m_scene.m_bg.m_phylayer.m_phydata[tileY
				* m_scene.m_bg.m_columns + tileX];

		if (collideNum == Background.PHY_BLOCK) {
			if (m_vX < 0) {
				if ((tileX + 1) * m_scene.m_bg.m_tileW - m_posX > 0) {
					m_vX = Math.min(m_vX, (tileX + 1) * m_scene.m_bg.m_tileW - m_posX);
				}
				else if ((tileX + 1) * m_scene.m_bg.m_tileW - m_posX < 0) {
					m_vX = Math.max(m_vX, (tileX + 1) * m_scene.m_bg.m_tileW - m_posX);
				}
				else {
					m_vX = 0;
				}
			}
			else if (m_vX > 0) {
				if (tileX * m_scene.m_bg.m_tileW - m_posX > 0) {
					m_vX = Math.min(m_vX, tileX * m_scene.m_bg.m_tileW - m_posX);
				}
				else if (tileX * m_scene.m_bg.m_tileW - m_posX < 0) {
					m_vX = Math.max(m_vX, tileX * m_scene.m_bg.m_tileW - m_posX);
				}
				else {
					m_vX = 0;
				}
			}

			if (m_vY < 0) {
				if ((tileY + 1) * m_scene.m_bg.m_tileH - m_posY > 0) {
					m_vY = Math.min(m_vY, (tileY + 1) * m_scene.m_bg.m_tileH - m_posY);
				}
				else if ((tileY + 1) * m_scene.m_bg.m_tileH - m_posY < 0) {
					m_vY = Math.max(m_vY, (tileY + 1) * m_scene.m_bg.m_tileH - m_posY);
				}
				else {
					m_vY = 0;
				}
			}
			else if (m_vY > 0) {
				if (tileY * m_scene.m_bg.m_tileH - m_posY > 0) {
					m_vY = Math.min(m_vY, tileY * m_scene.m_bg.m_tileH - m_posY);
				}
				else if (tileY * m_scene.m_bg.m_tileH - m_posY < 0) {
					m_vY = Math.max(m_vY, tileY * m_scene.m_bg.m_tileH - m_posY);
				}
				else {
					m_vY = 0;
				}
			}
		}

//		int collide = 0;

////		float collideX = m_posX + m_vX + m_collideBox[0];
////		float collideWidth = m_posX + m_vX + m_collideBox[2];
////		float collideY = m_posY + m_vY + m_collideBox[1];
////		float collideHeight = m_posY + m_vY + m_collideBox[3];
//		int startTileX = (int)collideX / m_scene.m_bg.m_tileW;
//		int startTileY = (int)collideY / m_scene.m_bg.m_tileH;
//		int endTileX = (int)collideWidth / m_scene.m_bg.m_tileW;
//		int endTileY = (int)collideHeight / m_scene.m_bg.m_tileH;
//		int i, j;
//		startTileX = Tools.limit(startTileX, 0, m_scene.m_bg.m_columns - 1);
//		endTileX = Tools.limit(endTileX, 0, m_scene.m_bg.m_columns - 1);
//		startTileY = Tools.limit(startTileY, 0, m_scene.m_bg.m_rows - 1);
//		endTileY = Tools.limit(endTileY, 0, m_scene.m_bg.m_rows - 1);
//
//		collide = 0;
////		int inx_i = -1, inx_j = -1;
//		for (i = startTileX; i <= endTileX; i++) {
//			for (j = startTileY; j <= endTileY; j++) {
//				int collideNum = m_scene.m_bg.m_phylayer.m_phydata[j
//						* m_scene.m_bg.m_columns + i];
//				if (collideNum == Background.PHY_BLOCK) {
//					if (i == startTileX && m_vX < 0) {
////						inx_i = i;
////						if ((i + 1) * m_scene.m_bg.m_tileW - m_collideBox[0] - m_posX > 0) {
////							m_vX = Math.min(m_vX, (i + 1) * m_scene.m_bg.m_tileW
////									- m_collideBox[0] - m_posX);
////						}
////						else {
////							m_vX = Math.max(m_vX, (i + 1) * m_scene.m_bg.m_tileW
////									- m_collideBox[0] - m_posX);
////						}
//						if ((i + 1) * m_scene.m_bg.m_tileW - m_posX > 0) {
//							m_vX = Math.min(m_vX, (i + 1) * m_scene.m_bg.m_tileW - m_posX);
//						}
//						else {
//							m_vX = Math.max(m_vX, (i + 1) * m_scene.m_bg.m_tileW - m_posX);
//						}
//						collide |= COL_LEFT;
//					}
//					else if (i == endTileX && m_vX > 0) {
////						inx_i = i;
////						if (i * m_scene.m_bg.m_tileW - m_collideBox[2] - m_posX >= 0) {
////							m_vX = Math.min(m_vX, i * m_scene.m_bg.m_tileW - m_collideBox[2] - m_posX);
////						}
////						else {
////							m_vX = Math.max(m_vX, i * m_scene.m_bg.m_tileW - m_collideBox[2] - m_posX);
////						}
//						if (i * m_scene.m_bg.m_tileW - m_posX >= 0) {
//							m_vX = Math.min(m_vX, i * m_scene.m_bg.m_tileW - m_posX);
//						}
//						else {
//							m_vX = Math.max(m_vX, i * m_scene.m_bg.m_tileW - m_posX);
//						}
//						collide |= COL_RIGHT;
//					}
//					if (j == startTileY && m_vY < 0) {
////						inx_j = j;
////						if ((j + 1) * m_scene.m_bg.m_tileH - m_collideBox[1] - m_posY > 0) {
////							m_vY = Math.min(m_vY, (j + 1) * m_scene.m_bg.m_tileH
////									- m_collideBox[1] - m_posY);
////						}
////						else {
////							m_vY = Math.max(m_vY, (j + 1) * m_scene.m_bg.m_tileH
////									- m_collideBox[1] - m_posY);
////						}
//						if ((j + 1) * m_scene.m_bg.m_tileH - m_posY > 0) {
//							m_vY = Math.min(m_vY, (j + 1) * m_scene.m_bg.m_tileH - m_posY);
//						}
//						else {
//							m_vY = Math.max(m_vY, (j + 1) * m_scene.m_bg.m_tileH - m_posY);
//						}
//						collide |= COL_TOP;
//					}
//					else if (j == endTileY && m_vY > 0) {
////						inx_j = j;
////						if (j * m_scene.m_bg.m_tileH - m_collideBox[3] - m_posY >= 0) {
////							m_vY = Math.min(m_vY, j * m_scene.m_bg.m_tileH
////									- m_collideBox[3] - m_posY);
////						}
////						else {
////							m_vY = Math.max(m_vY, j * m_scene.m_bg.m_tileH
////									- m_collideBox[3] - m_posY);
////						}
//						if (j * m_scene.m_bg.m_tileH - m_posY >= 0) {
//							m_vY = Math.min(m_vY, j * m_scene.m_bg.m_tileH - m_posY);
//						}
//						else {
//							m_vY = Math.max(m_vY, j * m_scene.m_bg.m_tileH - m_posY);
//						}
//						collide |= COL_BOTTOM;
//					}
//				}
//			}
//		}

//		if ((collide & COL_LEFT) != 0 && inx_i != -1) {
////			m_vX = (inx_i + 1) * m_scene.m_bg.m_tileW
////					- m_collideBox[0] - m_posX;
//			m_vX = 0;
//			if (m_vY > 0) {
//				m_vY = m_velocity;
//			}
//			if (m_vY < 0) {
//				m_vY = -m_velocity;
//			}
//		}
//		if ((collide & COL_RIGHT) != 0 && inx_i != -1) {
////			m_vX = inx_i * m_scene.m_bg.m_tileW - m_collideBox[2] - m_posX;
//			m_vX = 0;
//			if (m_vY > 0) {
//				m_vY = m_velocity;
//			}
//			if (m_vY < 0) {
//				m_vY = -m_velocity;
//			}
//		}
//		if ((collide & COL_TOP) != 0 && inx_j != -1) {
////			m_vY = (inx_j + 1) * m_scene.m_bg.m_tileH
////					- m_collideBox[1] - m_posY;
//			m_vY = 0;
//			if (m_vX > 0) {
//				m_vX = m_velocity;
//			}
//			if (m_vX < 0) {
//				m_vX = -m_velocity;
//			}
//		}
//		if ((collide & COL_BOTTOM) != 0 && inx_j != -1) {
////			m_vY = inx_j * m_scene.m_bg.m_tileH - m_collideBox[3] - m_posY;
//			m_vY = 0;
//			if (m_vX > 0) {
//				m_vX = m_velocity;
//			}
//			if (m_vX < 0) {
//				m_vX = -m_velocity;
//			}
//		}

//		int phy = -1;
//		int phy2 = -1;
//		if ((collide & COL_LEFT) != 0) { // 左边碰撞
//			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns + startTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(
//					startTileY - 1, 0, m_scene.m_bg.m_rows - 1)
//					* m_scene.m_bg.m_columns + startTileX];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posY -= m_velocity;
//					m_posY = Math.max((startTileY + 1) * m_scene.m_bg.m_tileH
//							- m_collideBox[3] - 1, m_posY);
//				}
//				else {
//					m_posY -= m_velocity;
//				}
//				return;
//			}
//			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns + startTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(endTileY + 1,
//					0, m_scene.m_bg.m_rows - 1)
//					* m_scene.m_bg.m_columns
//					+ startTileX];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posY += m_velocity;
//					m_posY = Math.min(endTileY * m_scene.m_bg.m_tileH
//							- m_collideBox[1], m_posY);
//				}
//				else {
//					m_posY += m_velocity;
//				}
//				return;
//			}
//		}
//
//		if ((collide & COL_RIGHT) != 0) { // 右边碰撞
//			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns + endTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(
//					startTileY - 1, 0, m_scene.m_bg.m_rows - 1)
//					* m_scene.m_bg.m_columns + endTileX];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posY -= m_velocity;
//					m_posY = Math.max((startTileY + 1) * m_scene.m_bg.m_tileH
//							- m_collideBox[3] - 1, m_posY);
//				}
//				else {
//					m_posY -= m_velocity;
//				}
//				return;
//			}
//			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns + endTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[Tools.limit(endTileY + 1,
//					0, m_scene.m_bg.m_rows - 1)
//					* m_scene.m_bg.m_columns
//					+ endTileX];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posY += m_velocity;
//					m_posY = Math.min(endTileY * m_scene.m_bg.m_tileH
//							- m_collideBox[1], m_posY);
//				}
//				else {
//					m_posY += m_velocity;
//				}
//				return;
//			}
//		}
//
//		if ((collide & COL_TOP) != 0) { // 上边碰撞
//			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns + startTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns
//					+ Tools.limit(startTileX - 1, 0, m_scene.m_bg.m_columns - 1)];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posX -= m_velocity;
//					m_posX = Math.max((startTileX + 1) * m_scene.m_bg.m_tileW
//							- m_collideBox[2] - 1, m_posX);
//				}
//				else {
//					m_posX -= m_velocity;
//				}
//				m_flipX = true;
//				return;
//			}
//			phy = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns + endTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[startTileY
//					* m_scene.m_bg.m_columns
//					+ Tools.limit(endTileX + 1, 0, m_scene.m_bg.m_columns - 1)];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posX += m_velocity;
//					m_posX = Math.min(endTileX * m_scene.m_bg.m_tileW
//							- m_collideBox[0], m_posX);
//				}
//				else {
//					m_posX += m_velocity;
//				}
//				m_flipX = false;
//				return;
//			}
//		}
//
//		if ((collide & COL_BOTTOM) != 0) { // 下边碰撞
//			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns + startTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns
//					+ Tools.limit(startTileX - 1, 0, m_scene.m_bg.m_columns - 1)];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posX -= m_velocity;
//					m_posX = Math.max((startTileX + 1) * m_scene.m_bg.m_tileW
//							- m_collideBox[2] - 1, m_posX);
//				}
//				else {
//					m_posX -= m_velocity;
//				}
//				m_flipX = true;
//				return;
//			}
//			phy = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns + endTileX];
//			phy2 = m_scene.m_bg.m_phylayer.m_phydata[endTileY
//					* m_scene.m_bg.m_columns
//					+ Tools.limit(endTileX + 1, 0, m_scene.m_bg.m_columns - 1)];
//			if (phy == -1 || phy2 == -1) {
//				if (phy == -1 && phy2 != -1) {
//					m_posX += m_velocity;
//					m_posX = Math.min(endTileX * m_scene.m_bg.m_tileW
//							- m_collideBox[0], m_posX);
//				}
//				else {
//					m_posX += m_velocity;
//				}
//				m_flipX = false;
//				return;
//			}
//		}
	}

	/**
	 * flag标记中ACTOR_FLAG_SELF是否被勾选
	 * 
	 * @return
	 */
	public boolean isSelfActor()
	{
		return (m_flag & Actor.ACTOR_FLAG_SELF) != 0;
	}

	public void loadAnimations(Resource res) throws IOException
	{
		m_anims = new Animation[m_refsResHead.length];
		for (int i = 0; i < m_anims.length; i++) {
			m_anims[i] = (Animation) (ResourceManager.getInstance()
					.loadResource(res, m_refsResHead[i]));
		}
		if (m_anims.length > 0) {
			m_anim = m_anims[0];
		}
	}

	@Override
	public synchronized void updateComponent(ResHead head, DWSerializable component)
	{
		if (m_loadFinished)
			return;
		if (head.m_type != DWSerializableFactory.RESOURCE_TYPE_ANIM)
			return;
		if (m_refsResHead == null || m_refsResHead.length == 0)
			return;
		for (int i = 0; i < m_refsResHead.length; i++) {
			if (m_refsResHead[i] != null && m_refsResHead[i].equals(head)) {
				m_anims[i] = (Animation) component;
				checkLoadFinished();
			}
		}
	}

	@Override
	public void checkLoadFinished()
	{
	}

	@Override
	public synchronized void dispose()
	{
		disposeHead();

		if (m_anims != null) {
			for (int i = 0; i < m_anims.length; i++) {
				if (m_anims[i] != null) {
					m_anims[i] = null;
				}
			}
			m_anims = null;
		}

		m_anim = null;
//		m_collideBox = null;
//		m_activeBox = null;
//		m_parameters = null;
	}

	/**
	 * 销毁该Actor，包括其动画文件
	 */
	public synchronized void destroy()
	{
		disposeHead();
		if (m_anims != null) {
			for (int i = 0; i < m_anims.length; i++) {
				try {
					if (m_anims[i] != null && animDestory(m_anims[i])) {
						m_anims[i].destroy();
						m_anims[i].m_resource.remove(m_anims[i].m_resHead);
						m_anims[i] = null;
					}
				}
				catch (Exception e) {
					Tools.debug(e);
				}
			}
			m_anims = null;
		}
		m_anim = null;
//		m_collideBox = null;
//		m_activeBox = null;
//		m_parameters = null;
	}

	public boolean animDestory(Animation anim){
		if(anim == null) {
			return false;
		}
		if (m_resource.isUseing(this, anim.m_resHead)) {
			return false;
		}
		if(m_scene != null){
			Enumeration<GameActor> enumeration = m_scene.m_dynamicActors.elements();
			DWSerializable obj;
			while (enumeration.hasMoreElements()) {
				obj = enumeration.nextElement();
				if (obj != null && obj != this ) {
					if (obj.isContainRefHead(anim.m_resHead)) {
						return false;
					}
				}
			}
		}
		return true;
	}
	
	@Override
	public boolean isContainRefHead(ResHead head)
	{
		if (head.m_type != DWSerializableFactory.RESOURCE_TYPE_ANIM)
			return false;
		return super.isContainRefHead(head);
	}

	public int getFrameAmount(int actionId)
	{
		try {
			int frameMax = 0;
			for (int i = 0; i < m_anim.m_actions[actionId].length; i++) {
				frameMax += (m_anim.m_actions[actionId][i] & 0xFFFF) >> 10;
			}
			return frameMax;
		}
		catch (Exception e) {
			Tools.debug(e);
			return 0;
		}
	}

}
